import { Player } from "../struct/Player";
import { CEHuangXiaoxiGlobal } from "../excel_extend/CEHuangXiaoxiGlobal";

/**
 * 庭院系统服务端数据处理
 */
export abstract class SDGarden {
    /**
     * 获取玩家当前水滴恢复倒计时
     * @param player 玩家
     * @returns 倒计时(秒) 剩余时间(秒)
     */
    public static getPlayerWaterCountdown(player: Player): number {
        let waterCount = 0;
        let waterRefreshTime = CEHuangXiaoxiGlobal.getWaterRefreshTime();
        let maxWaterCount = CEHuangXiaoxiGlobal.getMaxWaterCount();
        let lastWaterTime = Math.floor(player.gardenManager.lastWaterTime/1000);
        let currentTime = Math.floor(Date.now()/1000);
        //如果最近浇水时间大于当前时间，则返回0
        let timeDiff = currentTime - lastWaterTime;
        //如果时间差大于0，则计算水滴恢复倒计时
        let countdown = 0;
        if(timeDiff > 0){
            //计算水滴数量
            waterCount = Math.floor(timeDiff / waterRefreshTime);
            if(waterCount >= maxWaterCount){
                waterCount = maxWaterCount;
                countdown = 0;
            }else{
                countdown = waterRefreshTime-(timeDiff % waterRefreshTime);
            }
        }
        return countdown;
    }
    /**
     * 获取玩家当前水滴数量 通过最近浇水时间计算水滴数量
     */
    public static getPlayerWaterCount(player: Player): number {
        let waterCount = 0;
        let waterRefreshTime = CEHuangXiaoxiGlobal.getWaterRefreshTime();
        let maxWaterCount = CEHuangXiaoxiGlobal.getMaxWaterCount();
        let lastWaterTime = Math.floor(player.gardenManager.lastWaterTime/1000);
        let currentTime = Math.floor(Date.now()/1000);
        let timeDiff = currentTime - lastWaterTime;
        if(timeDiff > 0){
            waterCount = Math.floor(timeDiff / waterRefreshTime);
            if(waterCount > maxWaterCount){
                waterCount = maxWaterCount;
            }
        }
        return waterCount;
    }
    /**
     * 减少玩家水滴数量 通过计算更新最近浇水时间
     * @param player 玩家
     * @param waterCount 减少的水滴数量
     * @returns 
     */
    public static reducePlayerWaterCount(player: Player,waterCount: number): void {
        //获取玩家当前水滴数量
        let currentWaterCount = this.getPlayerWaterCount(player);
        let waterRefreshTime = CEHuangXiaoxiGlobal.getWaterRefreshTime();
        let maxWaterCount = CEHuangXiaoxiGlobal.getMaxWaterCount();
        if (currentWaterCount >= maxWaterCount) {
            player.gardenManager.lastWaterTime = Date.now() - (currentWaterCount - waterCount) * waterRefreshTime * 1000;
        } else {
            //不要重置获得水滴的剩余时间
            player.gardenManager.lastWaterTime = player.gardenManager.lastWaterTime + waterCount * waterRefreshTime * 1000;
        }
    }
    /**
     * 判断玩家是否可以浇水
     * @param player 玩家
     * @returns 
     */
    public static canWater(player: Player): any {
        if (player.gardenManager.plantStatus == 2) {
            return {
                c: 5100,
                m: "当前植物已成熟，请先收获"
            };
        }
        if (player.gardenManager.plantStatus == 3) {
            return {
                c: 5101,
                m: "当前植物已成熟，请种植下一棵树"
            };
        }
        let waterCount = this.getPlayerWaterCount(player);
        //获取浇水消耗
        let waterCost = CEHuangXiaoxiGlobal.getWaterCost();
        if(waterCount < waterCost){
            return {
                c: 5001,
                m: "水滴不足"
            };
        }
        return {
            c: 0,
            e: "可以浇水"
        };
    }
    /**
     * 判断玩家是否可以种植
     * @param player 玩家
     * @returns 
     */
    public static canPlant(player: Player): any {
        if (player.gardenManager.plantStatus == 1) {
            return {
                c: 5006,
                m: "当前植物正在生长"
            };
        }
        if (player.gardenManager.plantStatus == 2) {
            return {
                c: 5100,
                m: "当前植物已成熟，请先收获"
            };
        }

        return {
            c: 0,
            e: "可以种植"
        };
    }

}
